// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var leftMenu = require("../leftMenu/leftMenu");
	var css_m = require("../main/platform_main.css");
	var dialog = require("dialog");
	var databind = require("databind");
	var validate = require("validate");
	var tools =require("tools");
	var js_platform_main = require("./platform_main.js");
	var css_supervisionRelieveList = require("./supervisionRelieveList.css");
	var html_supervisionEndall = require("./supervisionEndall.html");
	var html_signed_detail_information = require("./html_signed_detail_information.html");
	var constant = require("constant");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var left;
	var inventoryId;
	var init = function(page) {
		left = page.params.left || base.getUrlParam("left");
		$(".left").removeClass("none");
		leftMenu.init(6);
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		$("#main").css("background", "#ffffff");
		$("#leftMenu7").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu7 .leftMenu li:first-child").addClass("leftMenu-open");
		$("#leftMenu7").find(".leftMenu-item").eq(left).addClass("leftMenu-active");
		js_platform_main.init(0);
		inquireBulkstockInventoryPageList();
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//点击首页
		$(".platform_main").unbind("click").click(function() {
			top.location.href = "../main/platform_main.html";
		});

		$("#supervisionRelieveList .searchButon").unbind("click").click(function() {
			pageIndex = 1;
			if ($("#supervisionRelieveList .searchStatus").val() == 1) {
				inquireBulkstockInventoryPageList1();
			} else {
				inquireBulkstockInventoryPageList();
			}
		});
	};

	//显示质押终结 
	var showSupervisionEndall = function(financeId) {
			$("#popdialog").html(html_supervisionEndall);
			var supervisionEndall_dialog = $("#supervisionEndall_dialog");
			dialog.showDialog(supervisionEndall_dialog, function() {});
			databind.fillForm($(".supervisionEndallContent"), {
				financeId: financeId
			});
			$("#sure_yes_supervisionEndall").unbind("click").click(function() {

				databind.submitDom($(".supervisionEndallContent"), function() {
					base.alert(1, "质押终结成功");
					dialog.hideDialog($("#supervisionEndall_dialog"));
					router.route("main/supervisionRelieveList.html?&left=10", true);
				});
			});

		}
		//查询货箱列表
	var inquireBulkstockInventoryPageList = function() {
		var productType=$("#supervisionRelieveList select[name=productType]").val();
		base.fnAjax({
			action: "inquireBulkstockInventoryPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				inventoryId: $("#relieves").val().split(','),
				financeNumber: $("#supervisionRelieveList .financeNumber").val(),
				waybillNumber: $("#supervisionRelieveList .waybillNumber").val(),
				enterpriseName: $("#supervisionRelieveList .enterpriseName").val(),
				bulkstockName: $("#supervisionRelieveList .bulkstockName").val(),
				productType: productType,
				caseNumber: $("#supervisionRelieveList .caseNumber").val()
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#bulkstockInventoryTpl").html();
					var html = juicer(tpl, data);
					$("#supervisionRelieveList .table").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#supervisionRelieveList .pagerCon"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireBulkstockInventoryPageList();
						});
					$(".supervisionRelieve").unbind("click").click(function() {
						var inventoryId = [];
						var financeId = [];
						var $this = $(this).parents("tr.tr_style");
						inventoryId.push($this.attr("inventoryId"));
						financeId.push($this.attr("financeId"));
						showSupervisionRelieve(inventoryId, financeId);
					});

					$(".signedInfoDetail").unbind("click").click(function() {
						$("#popdialog").html(html_signed_detail_information);
						dialog.showDialog($("#signed_detail_dialog"));
						inquireBulkstockInventoryDetail($(this).parents("tr").attr("inventoryId"));
					});

					$(".detainedApplication").unbind("click").click(function() {
						router.route("detainedApplication/accountDetainedApplication.html?deliveryId=" + $(this).attr("deliveryId") + "&financeId=" + $(this).attr("financeId"));
					});

					//点击详情
					$("#supervisionRelieveListBody").on("click", ".detail", function() {
						var deliveryId;
						deliveryId = $(this).parents("tr").attr("deliveryId");

						router.route("main/supervisionRelieve.html?deliveryId=" + deliveryId + "&left=" + left, true);
					});
					
					tools.changeWarningColor($("#supervisionRelieveList .bulkstockInventoryTBody"), "supervisionRelieveCreateDatetime");
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	
	var inquireBulkstockInventoryPageList1 = function() {
		var productType = $("#supervisionRelieveList select[name=productType]").val();
		base.fnAjax({
			action: "inquireBulkstockInventoryPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				financeNumber: $("#supervisionRelieveList .financeNumber").val(),
				waybillNumber: $("#supervisionRelieveList .waybillNumber").val(),
				enterpriseName: $("#supervisionRelieveList .enterpriseName").val(),
				bulkstockName: $("#supervisionRelieveList .bulkstockName").val(),
				productType: productType,
				caseNumber: $("#supervisionRelieveList .caseNumber").val()
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#bulkstockInventoryTpl").html();
					var html = juicer(tpl, data);
					$("#supervisionRelieveList table").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#supervisionRelieveList .pagerCon"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireBulkstockInventoryPageList1();
						});
					$(".supervisionRelieve").unbind("click").click(function() {
						var inventoryId = [];
						var financeId = [];
						var $this = $(this).parents("tr.tr_style");
						inventoryId.push($this.attr("inventoryId"));
						financeId.push($this.attr("financeId"));
						showSupervisionRelieve(inventoryId, financeId);
					});

					$(".signedInfoDetail").unbind("click").click(function() {
						$("#popdialog").html(html_signed_detail_information);
						dialog.showDialog($("#signed_detail_dialog"));
						inquireBulkstockInventoryDetail($(this).parents("tr").attr("inventoryId"));
					});

					$(".detainedApplication").unbind("click").click(function() {
						router.route("detainedApplication/accountDetainedApplication.html?deliveryId=" + $(this).attr("deliveryId") + "&tallyingId=" + $(this).attr("tallyingId"));
					});

					//点击详情
					$("#supervisionRelieveListBody").on("click", ".detail", function() {
						var deliveryId;
						deliveryId = $(this).parents("tr").attr("deliveryId");

						router.route("main/supervisionRelieve.html?deliveryId=" + deliveryId + "&left=" + left, true);
					});
					
					tools.changeWarningColor($("#supervisionRelieveList .bulkstockInventoryTBody"), "supervisionRelieveCreateTime");
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	//获取签收信息明细
	var inquireBulkstockInventoryDetail = function(inventoryId) {
		base.fnAjax({
			action: "inquireBulkstockInventoryDetail.json",
			async: false,
			oData: {
				inventoryId: inventoryId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#sigendDetailTpl").html();
					var html = juicer(tpl, data);
					$("#sigendDetailBody").html(html);
					
					if (data.datas[0].type == 1) {
						$("#signed_detail_dialog .signPreFile").html("出库照片：");
						$("#signed_detail_dialog .signSuffixFile").addClass("none");
					} else {
						tools.showLogisticsTrajectory(data.datas[0].caseNumber, data.datas[0].waybillNumber, $("#signed_detail_dialog .logisticsDetailInfo"));
					}
					
					
					var signPreFile = data.datas[0].signPreFile.split(',');
					var signSuffixFile = data.datas[0].signSuffixFile.split(',');
					$.each(signPreFile, function() {
						var _this = this;
						if (_this != "") {
							$("#signed_detail_dialog .signPreFile").append("<a target='_blank' href='/finance/api/v1.0/downloadFile.file?fileUuid=" + _this + " '><img src='/finance/api/v1.0/downloadFile.file?fileUuid=" + _this + " '/></a>")
						}
					});
					$.each(signSuffixFile, function() {
						var _this = this;
						if (_this != "") {
							$("#signed_detail_dialog .signSuffixFile").append("<a target='_blank' href='/finance/api/v1.0/downloadFile.file?fileUuid=" + _this + " '><img src='/finance/api/v1.0/downloadFile.file?fileUuid=" + _this + " '/></a>")
						}
					});
					
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	//显示解除监管弹出框
	var showSupervisionRelieve = function(inventoryId, financeId) {
		dialog.showDialog($("#supervisionRelieve_dialog"));
		databind.fillForm($(".supervisionRelieveContent"), {
			inventoryId: inventoryId
		});
		$("#sure_yes_supervisionRelieve").unbind("click").click(function() {

			databind.submitDom($(".supervisionRelieveContent"), function(data) {
				base.alert(1, "解除监管成功");
				js_platform_main.init();
				dialog.hideDialog($("#supervisionRelieve_dialog"));
				if (data.datas[0].endall) {
					showSupervisionEndall(financeId);
				} else {
					router.route("main/supervisionRelieveList.html?&left=10", true);
				}
			});
		});
	};
	return {
		init: init
	};
});